package util;

import java.util.Map;

import play.cache.Cache;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.Transform;

import controllers.Contact;

public class FsUtil {

	public static String convertUTF8TOASCII(String title){
		String ascii = java.text.Normalizer.normalize(title, java.text.Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+","");
		return ascii.replaceAll("([\\., '\"])", "-");
	}

	/**
	 * @param imagesService
	 * @param oldImage
	 * @return
	 */
	public static byte[] scaleImage(ImagesService imagesService, Image oldImage, int height, int width) {
		Transform resize = ImagesServiceFactory.makeResize(height, width);
		Image newImage = imagesService.applyTransform(resize, oldImage);
		return newImage.getImageData();
	}
	
	public static void deleteCacheProduct(){
		Cache.delete(Contant.LIST_PRODUCTS);
		Cache.delete(Contant.LIST_ACTIVE_PRODUCTS);
		Cache.delete(Contant.LIST_HIDDEN_PRODUCTS);
	}
	
	public static void deleteCacheProductFollowCategory(String category){
		Cache.delete(Contant.CATEGORY_PRODUCT + category);
	}
	
	//paging
	public static PaginationInfo pagingInAdmin(int page, int total, String action){
		PaginationInfo paginationInfo = new PaginationInfo(
				null,
				action, // action
				page, // current page
				Contant.PAGE_ITEM, // items per page
				total, // total number of results
				new String[]{ "query", "someParam" } // query params to be included in the links
			);
		return paginationInfo;
	}
	
	public static String fullURL(String staticUrl, String action,Map<String, Object> args) {
		if(staticUrl != null) {
			return staticUrl + "/" + args.get("page");
		}
		return play.mvc.Router.getFullUrl(action, args);
	}
	
	/**
	 * remove image
	 */
	public static void removeBlob(String path) {
		System.out.println(">>>>>>>>>path remove" + path);
		if(path != null) {
			BlobKey key = new BlobKey(path);
			BlobstoreService blobStoreService = BlobstoreServiceFactory.getBlobstoreService();
			blobStoreService.delete(key);
		}
	}
	
}
